package com.trainsmart.smartmvc
{
	import com.trainsmart.smartmvc.conf.Action;
	import com.trainsmart.smartmvc.conf.Flow;
	import com.trainsmart.smartmvc.conf.Step;
	
	import flash.events.Event;
	
	import mx.containers.HBox;
	import mx.containers.VBox;
	import mx.controls.Button;
	import mx.events.FlexEvent;	
	
	public class FlowContainer extends VBox
	{
		
		private var currentFlow:Flow = null;
		private var context:Context = null;
		private var currectStep:Step = null;
		private var currectForm:SmartForm = null;
		
		private var form:VBox = new VBox();
		private var panel:HBox = new HBox();
		public function FlowContainer()
		{
			this.addChild(form);
			form.setStyle("width","100%");
			form.setStyle("height","80%");			
			panel.setStyle("backgroundColor","0xFFFFFF");
			
			this.addChild(panel);
			panel.setStyle("width","100%");
			panel.setStyle("height","20%");
			panel.setStyle("backgroundColor","0xB8C12C");			
		}
		
		public function set flowId(value:String):void
		{
				loadFlow(value);
		}
		
		public function loadFlow(flowId:String, request:Object=null, response:Function=null):void
		{
			
			var context:Context = ContextManager.getInstance().findout(this);	
			var util:FlowUtil = new FlowUtil(context);		
			var flow:Flow = util.metaflows[flowId] as Flow;			
			this.context = context;
			this.currentFlow = flow;		
			forwardStep(flow.start,null,request,response);			
			// use default value
		}
		
		
		
		public function forwardStep(stepId:String,currentId:String, request:Object=null, response:Function=null ):void
		{
			var previewId:String = null;
			if(currectForm!=null)
				previewId = currectForm.viewId;
			
			var step:Step = currentFlow.steps[stepId] as Step;			
			var smartForm:SmartForm = context.getView(step.view);
			form.removeAllChildren();
			form.addChild(smartForm);
			this.currectStep = step;
			this.currectForm = smartForm;
			/*
			if(request!=null)
				currectForm.data = request;	
			*/	
			if(currentId!=null)
				currectForm.execute(currentId, request,response);
			
					
			//need to add button
			panel.removeAllChildren();
			var actions:Array = step.actions;			
			for (var actionId:String in actions)
			{
			    var action:Action = actions[actionId];
			    var button:Button = new Button();
			   	button.label = action.name;
				button.addEventListener(FlexEvent.BUTTON_DOWN,btnClick);
				panel.addChild(button);
			}
			
			 function btnClick(evt:Event):void{
				 var btn:Button = evt.target as Button;
				 var actions:Array = currectStep.actions;			
				 for (var actionId:String in actions)
				 {
				    var action:Action = actions[actionId];
				    if(action.name==btn.label){			    	
				 		doAction(action.id,request,response);   	
				    }
				 }	
			}
		}
		
		
		
		public function doAction(actionId:String,request:Object=null ,response:Function=null):void
		{
			 var action:Action = this.currectStep.actions[actionId] as Action;
			 if(action.getOption()==Action.OPTION_EXECUTE_CONTROL){
			 	var control:IControl = context.getControl(action.control) as IControl;
			 	context.putInSession("flow-container", this);			 				 				 	
			 	control.execute(request, response);
			 }else{			 				 	
			 	var stepId:String = action.step;			 	
			 	this.forwardStep(stepId, this.currectForm.viewId, request,response);			 
			 }
		}
		

	}
}